package com.haojiang.mh.remote.adapter


import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import cn.com.heaton.blelibrary.ble.utils.ThreadUtils
import com.google.android.material.tabs.TabLayout
import com.haojiang.mh.AppContext
import com.haojiang.mh.R
import com.haojiang.mh.remote.bean.AreaValue
import com.haojiang.mh.remote.utils.BleUtil.Companion.onTouchListener
import com.haojiang.mh.remote.widget.MotorItemView
import com.haojiang.mh.remote.widget.RmText


class MotorFragAdapter : RecyclerView.Adapter<MotorFragAdapter.MotorFragViewHolder>() {

    private val cmdList: MutableList<String> = mutableListOf()
    private var tabLayout: TabLayout? = null
    private val dataMap: MutableMap<Int, MutableList<AreaValue>> = mutableMapOf()
    private var context: Context = AppContext.instance()
    private var MOTOR_UP = context.getString(R.string.motor_up)
    private var MOTOR_DOWN = context.getString(R.string.motor_down)


    fun setData(map: MutableMap<Int, MutableList<AreaValue>>, tabLayout: TabLayout) {
        ThreadUtils.ui {
            cmdList.clear()
            notifyDataSetChanged()
            dataMap.clear()
            for ((key, value) in map) {
                value.forEach {
                    it.code?.let { it -> cmdList.add(it) }
                    dataMap.put(key, value)
                }
            }
            notifyDataSetChanged()
            this.tabLayout = tabLayout
        }
    }

    override fun getItemCount(): Int {
//        return viewTypeList.size
        return 1
    }


    val onMotorTouchListener = object : View.OnTouchListener {



        override fun onTouch(view: View?, event: MotionEvent?): Boolean {

//                LogUtil.e("按下了" + view.toString())
//            if (event != null && view != null && view is RmImageView) {
            if (event != null && view != null && view is RmText) {

                when (event.action) {

                }
//
//                    MotionEvent.ACTION_DOWN -> {
//                        LogUtil.e("action down")
////                        val tab = tabLayout?.getTabAt(tabLayout!!.selectedTabPosition)
////                        val itemViewCode = tab?.tag.toString()
////                        val viewTag = view.tag
////                        when (itemViewCode.toInt()) {
////                            ITEM_HEAD -> {
////                                when (viewTag) {
////                                    MOTOR_UP -> {
////                                        view.code = HEAD_UP
////                                        view.img = context.getString(R.string.bed_head)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                    MOTOR_DOWN -> {
////                                        view.code = HEAD_DOWN
////                                        view.img = context.getString(R.string.bed_head)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                }
////                            }
////
////                            ITEM_FOOT -> {
////                                when (viewTag) {
////                                    MOTOR_UP -> {
////                                        view.code = FOOT_UP
////                                        view.img = context.getString(R.string.bed_foot)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                    MOTOR_DOWN -> {
////                                        view.code = FOOT_DOWN
////                                        view.img = context.getString(R.string.bed_foot)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                }
////                            }
////
////                            ITEM_PILLOW -> {
////                                when (viewTag) {
////                                    MOTOR_UP -> {
////                                        view.code = PILLOW_UP
////                                        view.img = context.getString(R.string.bed_pillow)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                    MOTOR_DOWN -> {
////                                        view.code = PILLOW_DOWN
////                                        view.img = context.getString(R.string.bed_pillow)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                }
////                            }
////
////                            ITEM_LUMBAR -> {
////                                when (viewTag) {
////                                    MOTOR_UP -> {
////                                        view.code = LUMBAR_UP
////                                        view.img = context.getString(R.string.bed_lumbar)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                    MOTOR_DOWN -> {
////                                        view.code = LUMBAR_DOWN
////                                        view.img = context.getString(R.string.bed_lumbar)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                }
////                            }
////
////                            ITEM_HEAD_FOOT_BOTH -> {
////                                when (viewTag) {
////                                    MOTOR_UP -> {
////                                        view.code = HEAD_FOOT_BOTH_UP
////                                        view.img = context.getString(R.string.bed_head_foot_both)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                    MOTOR_DOWN -> {
////                                        view.code = HEAD_FOOT_BOTH_DOWN
////                                        view.img = context.getString(R.string.bed_head_foot_both)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                }
////                            }
////
////                            ITEM_PILLOW_LUMBAR_BOTH -> {
////                                when (viewTag) {
////                                    MOTOR_UP -> {
////                                        view.code = PILLOW_LUMBAR_BOTH_UP
////                                        view.img = context.getString(R.string.bed_pillow_lumbar_both)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                    MOTOR_DOWN -> {
////                                        view.code = PILLOW_LUMBAR_BOTH_DOWN
////                                        view.img = context.getString(R.string.bed_pillow_lumbar_both)
////                                        view.pressType = context.getString(R.string.press_keep)
////                                        onActionDown(view)
////                                    }
////                                }
////                            }
////                        }
//                    }
//
//                    MotionEvent.ACTION_MOVE -> {
//                        LogUtil.e("已经开始滑动")
////                        if (inRangeOfView(view, event.rawX.toInt(), event.rawY.toInt())) {
////                            LogUtil.e("滑动抬起")
////                            onActionUp(view)
////                        }
//                    }
//
////                    MotionEvent.ACTION_CANCEL,
//                    MotionEvent.ACTION_UP -> {
//                        LogUtil.e("抬起")
//                        onActionUp(view)
//                    }
//                }
            }
            return false
        }
    }

    private fun inRangeOfView(view: View, screenX: Int, screenY: Int): Boolean {
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        val left = location[0]
        val top = location[1]
        val right = left + view.measuredWidth
        val bottom = top + view.measuredHeight
        return screenX < left || screenX > right || screenY < top || screenY > bottom
    }


    fun traverseMotorView(view: View, list: List<String>) {
        if (view is ViewGroup) {
            for (i in 0 until view.childCount) {
                traverseMotorView(view.getChildAt(i), list)
            }
            return
        }


        if (view is RmText && view.code != null) {
            if (view.tag != null && (view.tag.toString().equals(MOTOR_UP, ignoreCase = true) ||
                        view.tag.toString().equals(MOTOR_DOWN, ignoreCase = true))
            ) {
                view.setOnTouchListener(onMotorTouchListener)
            }
            else {
                view.visibility = View.GONE
                cmdList.forEach {
                    if (it.equals(view.code, ignoreCase = true)) {
                        view.visibility = View.VISIBLE
                        view.setOnTouchListener(onTouchListener)
                    }
                }
            }
        }
    }

    /**
     * 设置界面
     */
    override fun onBindViewHolder(holder: MotorFragViewHolder, position: Int) {

        traverseMotorView(holder.itemView, cmdList)

//        val viewType = viewTypeList.get(position)
//        val cmdList = dataMap.get(viewType)
////        LogUtil.e(cmdList.toString())
//        if (cmdList != null) {
//        }

//        cmd.code?.let {
//            when (cmd.code) {
//                CmdKey.HEAD_UP,
//                CmdKey.HEAD_DOWN -> {
//                    val itemView = holder.itemView as HeadItemView
//                }
//
//                CmdKey.FOOT_UP,
//                CmdKey.FOOT_DOWN -> {
//                    val itemView = holder.itemView as FootItemView
//                }
//            }
//        }


//        if (dataList.get(position).isClick) {
//            itemView.setData(data, true)
//        } else {
//            itemView.setData(data, false)
//        }

//        itemView.setOnClickListener(object : View.OnClickListener {
//            override fun onClick(p0: View?) {
//                for (i in 0 until dataList.size) {
//                    dataList.get(i).isClick = false
//                }
//                dataList.get(position).isClick = true
//                notifyDataSetChanged()
////                iMotorItemClickListener?.onItemClick(dataList.get(position))
//            }
//        })
    }

    override fun getItemViewType(position: Int): Int {
//        return viewTypeList.get(position)
        return 0
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MotorFragViewHolder {
        return MotorFragViewHolder(MotorItemView(parent.context))
//        when (viewType) {
//            ITEM_HEAD -> {
//                return MotorFragViewHolder(
//                    HeadItemView(parent.context)
//                )
//            }
//
//            ITEM_FOOT -> {
//                return MotorFragViewHolder(
//                    FootItemView(parent.context)
//                )
//            }
//
//            ITEM_PILLOW -> {
//                return MotorFragViewHolder(
//                    PillowItemView(parent.context)
//                )
//            }
//
//            ITEM_LUMBAR -> {
//                return MotorFragViewHolder(
//                    LumbarItemView(parent.context)
//                )
//            }
//
//            ITEM_HEAD_FOOT_BOTH -> {
//                return MotorFragViewHolder(
//                    HeadFootBothItemView(parent.context)
//                )
//            }
//
//            ITEM_PILLOW_LUMBAR_BOTH -> {
//                return MotorFragViewHolder(
//                    PillowLumbarBothItemView(parent.context)
//                )
//            }
//
//            ITEM_UBL_ON_OFF -> {
//                return MotorFragViewHolder(
//                    UblOnOffItemView(parent.context)
//                )
//            }
//
//            ITEM_UBL_OFF -> {
//                return MotorFragViewHolder(
//                    UblOffItemView(parent.context)
//                )
//            }
//        }
//        return MotorFragViewHolder(
//            HeadItemView(parent.context)
//        )
    }


    /**
     * adapter的ViewHolder类
     */
    class MotorFragViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {}

}
